//
//  MyTableViewController.swift
//  WeiPaiHui
//
//  Created by joke on 16/1/21.
//  Copyright © 2016年 joke. All rights reserved.
//

import UIKit

class MyTableViewController: UITableViewController {

    
    override func viewDidLoad() {
        super.viewDidLoad()
        

        self.tableView.registerClass(nil, forCellReuseIdentifier: "reuseIdentifier")
        let headerView = UIView(frame: CGRect(x: 0,y:0,width: self.view.frame.width,height: self.view.frame.height/5*2))
        let headerViewHeight = self.view.frame.height/3
        var setButton:UIButton = UIButton(frame: CGRect(x: self.view.frame.width-40, y:30 , width: 24, height: 24))
        setButton.setImage(UIImage(named: "clock"), forState: .Normal)
        
        setButton.addTarget(self, action: "goToSet", forControlEvents: .TouchUpInside)

        headerView.addSubview(setButton)
        
//        let leftLabel:UILabel = UILabel(frame: CGRect(x: self.view.frame.width/3, y: 75, width: 100, height: 100))
//        
//        leftLabel.text = "成交数"
//        let leftLabelText:NSString = leftLabel.text!
//        var leftTextSize = leftLabelText.sizeWithAttributes([NSFontAttributeName:leftLabel.font])
//        print(leftTextSize)
//        headerView.addSubview(leftLabel)
        
        let middleLabel:UILabel = UILabel()
        let middleCount:UILabel = UILabel()
        middleLabel.text = "累计成交额"
        middleCount.text = "178万"
        //计算字体size
        let labelText:NSString = middleLabel.text!
        var textSize = labelText.sizeWithAttributes([NSFontAttributeName:middleLabel.font])
        print(textSize)
        middleLabel.frame = CGRect(x: self.view.frame.width/2-textSize.width/2, y:headerViewHeight/2 , width: textSize.width, height: textSize.height)
        middleCount.frame = middleLabel.frame
        middleCount.frame.origin.y = middleLabel.frame.origin.y - 25
        headerView.addSubview(middleCount)
        headerView.addSubview(middleLabel)
        
//        let rightLabel:UILabel = UILabel(frame: CGRect(x: self.view.frame.width/3*2, y: 75, width: 100, height: 100))
//        rightLabel.text = "最高成交价"
//        headerView.addSubview(rightLabel)
//        let testView = UIView(frame: CGRect(x: self.view.frame.width/2,y:0,width: 10,height: 10))
//        testView.backgroundColor = UIColor.redColor()
//        headerView.addSubview(testView)
        
        headerView.backgroundColor = UIColor.redColor()
        print(self.tableView.frame)
        self.tableView.tableHeaderView = headerView

        // Uncomment the following line to preserve selection between presentations
        // self.clearsSelectionOnViewWillAppear = false

        // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
        // self.navigationItem.rightBarButtonItem = self.editButtonItem()
    }
    
    
    
    func goToSet() {
//        let vc = UIViewController()
//        
//        self.presentViewController(vc, animated: true) { () -> Void in
//            print(1)
//        }
        print(1)
        let view = DetailViewController(nibName:"DetailViewController",bundle: nil)  //注册自定义xib文件
//      设置跳转动画
        view.modalTransitionStyle = UIModalTransitionStyle.CrossDissolve
        self.presentViewController(view, animated: true, completion: nil)
        
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    // MARK: - Table view data source

    override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
        // #warning Incomplete implementation, return the number of sections
        return 5
    }

    override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        // #warning Incomplete implementation, return the number of rows
        switch section{
        case 0:return 3
        case 1:return 1
        case 2:return 1
        case 3:return 1
        case 4:return 1
        default:return 0
        }
    }
    
    override func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
        return 35
    }
    var stringArray:[String] = ["参拍中","已拍下","已结束"]
    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
//        let cell = tableView.dequeueReusableCellWithIdentifier("reuseIdentifier", forIndexPath: indexPath)
        let section = indexPath.section
        let cell:UITableViewCell = UITableViewCell(style: .Subtitle, reuseIdentifier: "reuseIdentifier")
        
        // Configure the cell...
        switch section {
        case 0:cell.textLabel?.text = self.stringArray[indexPath.row]
        case 1:cell.textLabel?.text = "我的提醒"
        case 2:cell.textLabel?.text = "我的保证金"
        case 3:cell.textLabel?.text = "我的关注"
        case 4:cell.textLabel?.text = "反馈及意见"
        default:cell.textLabel?.text = "none"
        }
        
        
        
        return cell
    }

    override func tableView(tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
        return 10
    }
    
    override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
        
        let view = DetailViewController(nibName:"DetailViewController",bundle: nil)  //注册自定义xib文件
        
        print(view.nibName)
        self.navigationController?.pushViewController(view, animated: true)
    }
    

    
//    override func tableView(tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
//        switch section{
//        case 0:return 200
//        default:return 0
//        }
//    }
//    
//    override func tableView(tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
//        let nilView:UIView = UIView()
//        nilView.backgroundColor = UIColor.redColor()
////        let myHeader:UIView = UIView(frame: CGRect(x: 0, y: 0, width: self.view.frame.width, height: 200))
////        
////        if section == 0 {
////            return nilView
////
////        }
//        //return UIView(frame: CGRect(x: 0, y: 0, width: 0, height: 0))
//        return nilView
//    }
//    
    /*
    // Override to support conditional editing of the table view.
    override func tableView(tableView: UITableView, canEditRowAtIndexPath indexPath: NSIndexPath) -> Bool {
        // Return false if you do not want the specified item to be editable.
        return true
    }
    */

    /*
    // Override to support editing the table view.
    override func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) {
        if editingStyle == .Delete {
            // Delete the row from the data source
            tableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation: .Fade)
        } else if editingStyle == .Insert {
            // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
        }    
    }
    */

    /*
    // Override to support rearranging the table view.
    override func tableView(tableView: UITableView, moveRowAtIndexPath fromIndexPath: NSIndexPath, toIndexPath: NSIndexPath) {

    }
    */

    /*
    // Override to support conditional rearranging of the table view.
    override func tableView(tableView: UITableView, canMoveRowAtIndexPath indexPath: NSIndexPath) -> Bool {
        // Return false if you do not want the item to be re-orderable.
        return true
    }
    */

    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
        // Get the new view controller using segue.destinationViewController.
        // Pass the selected object to the new view controller.
    }
    */

}
